<?php
App::uses('AppModel', 'Model');
/**
 * Project Model
 *
 * @property Domain $Domain
 */
class Project extends AppModel {
/**
 * Display field
 *
 * @var string
 */
	public $displayField = 'domain';
/**
 * Validation rules
 *
 * @var array
 */
	public $validate = array(
		'name' => array(
			'notempty' => array(
				'rule' => array('notempty'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		'domain' => array(
					'notempty' => array(
						'rule' => array('notempty'),
		//'message' => 'Your custom message here',
		//'allowEmpty' => false,
		//'required' => false,
		//'last' => false, // Stop validation after this rule
		//'on' => 'create', // Limit validation to 'create' or 'update' operations
		),
		),
	);

	//The Associations below have been created with all possible keys, those that are not needed can be removed

/**
 * hasAndBelongsToMany associations
 *
 * @var array
 */
	public $hasAndBelongsToMany = array(
		'Domain' => array(
			'className' => 'Domain',
			'joinTable' => 'domains_projects',
			'foreignKey' => 'project_id',
			'associationForeignKey' => 'domain_id',
			'unique' => 'keepExisting',
			'conditions' => '',
			'fields' => '',
			'order' => '',
			'limit' => '',
			'offset' => '',
			'finderQuery' => '',
			'deleteQuery' => '',
			'insertQuery' => '',
			'dependent' => true
		),
		'Authorization' => array(
			'className' => 'Authorization',
			'joinTable' => 'projects_authorizations',
			'foreignKey' => 'project_id',
			'associationForeignKey' => 'authorization_id',
			'unique' => 'keepExisting',
			'conditions' => '',
			'fields' => '',
			'order' => '',
			'limit' => '',
			'offset' => '',
			'finderQuery' => '',
			'deleteQuery' => '',
			'insertQuery' => '',
			'dependent' => true
		),
		'Link' => array(
			'className' => 'Link',
			'joinTable' => 'projects_links',
			'foreignKey' => 'project_id',
			'associationForeignKey' => 'link_id',
			'unique' => 'keepExisting',
			'conditions' => '',
			'fields' => '',
			'order' => '',
			'limit' => '',
			'offset' => '',
			'finderQuery' => '',
			'deleteQuery' => '',
			'insertQuery' => '',
			'dependent' => true
		),
		'Job' => array(
				'className' => 'Job',
				'joinTable' => 'projects_jobs',
				'foreignKey' => 'project_id',
				'associationForeignKey' => 'job_id',
				'unique' => 'keepExisting',
				'conditions' => '',
				'fields' => '',
				'order' => '',
				'limit' => '',
				'offset' => '',
				'finderQuery' => '',
				'deleteQuery' => '',
				'insertQuery' => '',
				'dependent' => true
	),
		'User' => array(
				'className' => 'User',
				'joinTable' => 'projects_users',
				'foreignKey' => 'project_id',
				'associationForeignKey' => 'user_id',
				'unique' => 'keepExisting',
				'conditions' => '',
				'fields' => '',
				'order' => '',
				'limit' => '',
				'offset' => '',
				'finderQuery' => '',
				'deleteQuery' => '',
				'insertQuery' => '',
				'dependent' => false
	)
	);
	public $hasMany = array(
		'Keyword' => array(
				'className' => 'Keyword',
				'foreignKey' => 'project_id',
				'dependent' => true,
				'conditions' => '',
				'fields' => '',
				'order' => '',
				'limit' => '',
				'offset' => '',
				'exclusive' => '',
				'finderQuery' => '',
				'counterQuery' => ''
	));
	
	public function beforeDelete( $options = array() ) {
		
		$options_['joins'] = array(
		    array('table' => 'domains_projects',
		        'alias' => 'DomainsProject',
		        'type' => 'inner',
		        'conditions' => array(
		            'Domain.id = DomainsProject.domain_id'
		        )
		    ),
		    array('table' => 'projects',
		        'alias' => 'Project',
		        'type' => 'inner',
		        'conditions' => array(
		            'DomainsProject.project_id = Project.id'
		        )
		    )
		);
		$options_['conditions'] = array(
		    'Project.id' => $this->id
		);
		
		$conyt = $this->Domain->find('count',$options_);		
		return ($conyt == 0) ? true : false;
	}
	
}
